package com.asiainfo.dacp.resource.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.asiainfo.dacp.resource.model.ResourceModel;
import com.asiainfo.dacp.resource.vo.BrokerResource;

@Service
public class ResourceRedisService {
	
	@Autowired
    private RedisTemplate redisTemplate;
	@Autowired
	private ResourceModel resourceModel;
	
	private static String BROKER_RESOURCE_QUEUE_KEY = "datastash:broker_resource";
	
	 private static Logger LOG = LoggerFactory.getLogger(ResourceRedisService.class);
	
	public void pushBorkerResource(String brokerId,String brokerIp,String diskName){
		
		BrokerResource borkerResource = resourceModel.getResourceModel(brokerId, brokerIp, diskName);
		redisTemplate.opsForHash().put(BROKER_RESOURCE_QUEUE_KEY, brokerIp, borkerResource);
	}
	    
	public BrokerResource getBrokerResource(String brokerIp){
		
		BrokerResource borkerResource = null;
		try {
			
			borkerResource = (BrokerResource) redisTemplate.opsForHash().get(BROKER_RESOURCE_QUEUE_KEY, brokerIp);
			
		} catch (Exception e) {
			LOG.error("{}",e);
		}
		
		return borkerResource;
	}
	
	public List<BrokerResource> getBrokerResourceList(){
		
		List<BrokerResource> result = null;
		try {
			 result = redisTemplate.opsForHash().values(BROKER_RESOURCE_QUEUE_KEY);
		} catch (Exception e) {
			LOG.warn("获取brokerResource为空,{}",e);
		}
		return result;
		
	}

}
